package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.RecordTopUpInfo;
import com.sphd.beautysalon.info.TopUpInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.VipAddPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
* Created by Hu on 2016/12/07
*/

public class VipAddModelImpl implements VipAddModel, VolleyJsonObjectCallBack {
    VipAddPresenterImpl vipAddPresenter;
    @Override
    public void sendUrl(String url, VipAddPresenterImpl vipAddPresenter) {
        this.vipAddPresenter=vipAddPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void topuu(String s, VipAddPresenterImpl vipAddPresenter) {
        this.vipAddPresenter=vipAddPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res=response.getInt("res");
                    vipAddPresenter.sendRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            vipAddPresenter.sendError(error);
            }
        });
    }

    @Override
    public void sendRecodUrl(String s, VipAddPresenterImpl vipAddPresenter) {
        this.vipAddPresenter=vipAddPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    double account=response.getDouble("account");
                    List<RecordTopUpInfo> recordTopUpInfos =JsonUtil.getListFromString(response.getString("list"),RecordTopUpInfo.class);
                    vipAddPresenter.sendResList(account,recordTopUpInfos);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                vipAddPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<TopUpInfo> topUpInfos= JsonUtil.getListFromString(response.getString("res"),TopUpInfo.class);
            vipAddPresenter.sendList(topUpInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        vipAddPresenter.sendError(error);
    }
}